import request from "@/utils/requests";
import { AxiosProgressEvent, AxiosPromise } from "axios";
import { Result } from "@/model";

//获取服务器根目录文件
// export function listFileRoot():AxiosPromise<Result<HyFileBase>>{
//     return listFilePath('/');
// }
/**
 * 获取相对路径的文件
 * @param path 路径
 * @returns 
 */

export function listFilePath(path:string):AxiosPromise<Result<HyFileBase>>{
    return request({
        url:'/file?path='+path,
        method:'get'
    });
}

/**
 * 删除指定文件或文件夹
 * @param path 要删除的文件或文件夹
 * @returns 
 */
export function delFile(path:string):AxiosPromise<Result<any>>{
    return request({
        url:'/file/del?path='+path,
        method:'delete'
    });
}

/**
 * 重命名文件或文件夹
 * @param oldName 原文件名(包括文件夹)
 * @param newName 新文件名(包括文件夹)
 * @returns 
 */

export function renameFile(oldName:string,newName:string):AxiosPromise<Result<any>>{
    return request({
        url:'/file/ren?oldName='+oldName+"&newName="+newName,
        method:'put',
    });
}
/**
 * 上传文件
 * @param files 超过32M执行分片上传
 * @returns 
 */
export function uploadFileChunks(form:FormData,callback?:(progressEvent: AxiosProgressEvent) => void):AxiosPromise<Result<any>>{
    return request({
        url:'/file/upload/chunk',
        method:'post',
        data:form,
        headers:{
            'Content-Type':'multipart/form-data'
        },
        onUploadProgress:callback,
    });
}
/**
 * 上传文件
 * @param files 普通上传（文件小于32M）
 * @returns 
 */
export function uploadFile(form:FormData,callback?:(progressEvent: AxiosProgressEvent) => void):AxiosPromise<Result<any>>{
    return request({
        url:'/file/upload',
        method:'post',
        data:form,
        headers:{
            'Content-Type':'multipart/form-data'
        },
        onUploadProgress:callback,
    });
}
/**
 * 上传文件
 * @param files 分片上传之后合并文件
 * @returns 
 */
export function mergeFile(form:FormData):AxiosPromise<Result<any>>{
    return request({
        url:'/file/upload/merge',
        method:'post',
        data:form,
        headers:{
            'Content-Type':'multipart/form-data'
        }
    });
}
/**
 * 复制
 * @param name 待复制的文件或文件夹
 * @param newPath 目标文件夹
 * @returns 
 */
export function copyFile(name:string,newPath:string):AxiosPromise<Result<any>>{
    return request({
        url:'/file/copy?name='+name+"&newPath="+newPath,
        method:'put',
    });
}
/**
 * 剪切
 * @param name 待剪切的文件或文件夹
 * @param newPath 目标文件夹
 * @returns 
 */
export function cutFile(name:string,newPath:string):AxiosPromise<Result<any>>{
    return request({
        url:'/file/cut?name='+name+"&newPath="+newPath,
        method:'put',
    });
}
// export function deleteFileBatch(names:string[]){
//     return request({
//         url:'/file/batchDel',
//         method:'delete',
//         data:names,
//     });
// }
/**
 * 新建文件夹
 * @param name 文件夹名
 * @param path 当前文件夹
 * @returns 
 */
export function newDir(name:string,path:string):AxiosPromise<Result<any>>{
    return request({
        url:'/file/newDir?name='+name+"&path="+path,
        method:'put',
    });
}